<template>
  <Dialog ref="dialog" title="填报" width="800" class="tb-mh">
    <div style="padding: 24px 24px 16px 24px">
      <div style="margin: 0px 0 20px 0">
        {{
          studentData.studentName +
          '-' +
          studentInfo.province +
          '-' +
          (studentInfo.sex == 0 ? '男' : '女') +
          '-' +
          '    高考分数:' +
          studentInfo.score +
          '分' +
          `${studentData.isDzTypeDiff == 1 ? '    语数外总分:' + studentInfo.score_wx + '分' : ''}`
        }}
      </div>
      <el-table
        :data="tableData"
        stripe
        style="width: 100%"
        :header-cell-style="{ 'border-bottom': 'none' }"
        :header-row-style="{ height: '60px' }"
        :cell-style="{ height: '60px', 'border-bottom': 'none' }"
      >
        <el-table-column prop="batch" label="批次" align="center">
          <template #default="{ row }">
            <p>
              {{
                `${row.type == 1 ? row.batch + '(普高)' : row.type == 2 ? row.batch + '(中职)' : row.batch}`
              }}
            </p>
          </template>
        </el-table-column>
        <el-table-column prop="score" label="省控线" align="center" />
        <el-table-column fixed="right" label="操作" align="center">
          <template #default="scope">
            <el-button
              v-if="scope.row.score <= studentInfo.score"
              type="primary"
              size="small"
              @click="handleClick(scope.row, gkType)"
              >创建方案</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </Dialog>
</template>
<script setup>
import Dialog from '@/components/Dialog/Dialog.vue';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { batchInfo, checkRank } from '@/api/leader/Filling.js';
import { ElMessage, ElMessageBox } from 'element-plus';
const emit = defineEmits(['openSetScore']);
const router = useRouter();
const isGkbData = ref({});
const dialog = ref(null);
const tableData = ref([]);

// 父级传递的学生数据
const studentData = ref({});
// 学生信息
const studentInfo = ref({});
function handleClick(val) {
  // localStorage.setItem('finalSchemeStatus','0')
  localStorage.setItem('zhigao', val.type); //2代表中职 1 代表普高
  if (
    studentData.value.student.graduate_time != isGkbData.value.graduate_time &&
    isGkbData.value.isGKB == 1
  ) {
    ElMessage({
      type: 'error',
      message: '高考年份不匹配',
    });
  } else {
    if (studentData.value.student.score < val.score) {
      ElMessageBox.confirm(
        `学生分数低于当前批次${val.batch}线分数，是否继续填报?`,
        '提示',
        {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning',
        }
      )
        .then(() => {
          let body = {
            batchInfo: { ...val, score: '' },
            studentData: studentData.value,
          };

          let jsonStr = JSON.stringify(body);
          jsonStr = btoa(encodeURIComponent(jsonStr));

          if (val.batch == '高职单招' || val.batch == '中职单招') {
            router.push({
              path: '/leader/volunteerd',
              query: { data: jsonStr },
            });
          } else {
            router.push({
              path: '/leader/volunteer',
              query: { data: jsonStr },
            });
          }
        })
        .catch(() => {});
    } else {
      let body = {
        batchInfo: { ...val, score: '' },
        studentData: studentData.value,
      };

      const userScoreWX =
        body.studentData?.isDzTypeDiff &&
        body.batchInfo?.is_batch == 1 &&
        body.studentData?.student?.score_wx != '0';

      body['userScoreWX'] = userScoreWX;

      let jsonStr = btoa(encodeURIComponent(JSON.stringify(body)));

      if (val.batch == '高职单招' || val.batch == '中职单招') {
        router.push({
          path: '/leader/volunteerd',
          query: { data: jsonStr },
        });
      } else {
        // 先检查位次是否正确
        const obj = {
          pid: body.studentData?.student?.pid,
          course: body.studentData?.student?.course,
          bzType: body.batchInfo?.bzType,
          score: body.studentData?.student?.score,
          rank: body.studentData?.student?.section,
        };

        // 判断是否有文化课成绩和文化课校验字段
        if (
          body.studentData?.isDzTypeDiff &&
          body.batchInfo?.is_batch == 1 &&
          body.studentData?.student?.score_wx == '0'
        ) {
          ElMessageBox.confirm('您当前还没有设置语数外成绩?', '提示', {
            distinguishCancelAndClose: true,
            confirmButtonText: '去设置',
            cancelButtonText: '取消',
          })
            .then(() => {
              dialog.value.closeChange();
              emit('openSetScore', { id: body.studentData?.id });
            })
            .catch(() => {});
          return;
        } else {
          obj.score =
            body.batchInfo?.is_batch == 1 && body.studentData?.isDzTypeDiff == 1
              ? Number(body.studentData?.student?.score_wx)
              : body.studentData?.student?.score;
          checkRank(obj)
            .then((res) => {
              if (res.code == 20000) {
                if (res.data.status == 0) {
                  ElMessageBox.confirm(
                    '考生当前分数【' +
                      res.data.score +
                      '分，' +
                      res.data.rank +
                      '位】对应的系统位次【' +
                      res.data.srank +
                      '位】不匹配,是否现在修改？',
                    '提示',
                    {
                      
                      distinguishCancelAndClose: true,
                      confirmButtonText: '继续填报',
                      cancelButtonText: '立即修改',
                    }
                  )
                    .then(() => {
                      router.push({
                        path: '/leader/volunteer',
                        query: { data: jsonStr },
                      });
                    })
                    .catch((action) => {
                      console.log('用户选择了：' + action);
                      if (action == 'cancel') {
                        dialog.value.closeChange();
                        emit('openSetScore', {
                          id: body.studentData?.id,
                          formType: 'checkRank',
                        });
                      }
                    });
                  return;
                } else {
                  router.push({
                    path: '/leader/volunteer',
                    query: { data: jsonStr },
                  });
                }
              }
            })
            .catch(() => {});
        }
      }
    }
  }
}
let gkType = ref(null);
function openChange(val, isGkb) {
  console.log('填报类型', val);
  gkType.value = val.gkaProvinceName;
  isGkbData.value = isGkb;
  // 打开组件弹窗
  studentData.value = val;
  studentInfo.value = val.student;
  dialog.value.openChange();

  getBatchInfo();
}

// 获取批次线信息
function getBatchInfo() {
  let body = {
    provinceCode: studentData.value.gkaProvinceNumId,
    course: studentInfo.value.course,
    score: studentInfo.value.score,
  };
  batchInfo(body).then((res) => {
    tableData.value = res.data.batches;
    if (gkType.value == '四川') {
      tableData.value.push({
        batch: '高职单招',
        type: 1,
        course: '物理',
        id: 84,
        is_batch: 2,
        provinceCode: 51,
        score: 100,
      });
    }
    if (gkType.value == '四川') {
      tableData.value.push({
        batch: '中职单招',
        type: 2,
        course: '物理',
        id: 84,
        is_batch: 2,
        provinceCode: 51,
        score: 100,
      });
    }
  });
}

defineExpose({
  openChange,
});
</script>
<style lang="scss">
.tb-mh {
  .el-dialog {
    padding: 0;
  }
  .el-dialog__body {
    margin-top: 0;
    // padding: 24px 24px 16px 24px;
    .header {
      padding: 19px 30px;
    }
    .el-divider {
      margin: 0;
    }
  }
}
</style>
